package net.mingsoft.basic.util;

import cn.hutool.core.util.*;
import cn.hutool.core.io.*;
import cn.hutool.http.*;
import org.apache.commons.lang3.*;
import cn.hutool.json.*;
import org.springframework.core.io.support.*;
import org.springframework.core.io.*;
import java.io.*;
import org.lionsoul.ip2region.*;
import java.net.*;
import org.slf4j.*;

public class IpUtils
{
    private static final Logger LOG;
    public static final String IP_URL = "https://www.ip.cn/api/index?ip=%s&type=1";

    public static boolean internalIp(final String ip) {
        final byte[] addr = textToNumericFormatV4(ip);
        return internalIp(addr) || "127.0.0.1".equals(ip);
    }



    private static boolean internalIp(byte[] addr) {
        if (!ObjectUtil.isNull(addr) && addr.length >= 2) {
            byte b0 = addr[0];
            byte b1 = addr[1];
            final byte SECTION_1 = 10;
            final byte SECTION_2 = -84;
            final byte SECTION_3 = 16;
            final byte SECTION_4 = 31;
            final byte SECTION_5 = -64;
            final byte SECTION_6 = -88;
            switch(b0) {
                case -84:
                    if (b1 >= 16 && b1 <= 31) {
                        return true;
                    }
                case -64:
                    switch(b1) {
                        case -88:
                            return true;
                        default:
                            throw new IllegalStateException("Unexpected value: " + b1);
                    }
                case 10:
                    return true;
                default:
                    return false;
            }
        } else {
            return true;
        }
    }


    public static String getRealAddressByIp(final String ip) {
        String address = "XX XX";
        if (internalIp(ip)) {
            return "\u5185\u7f51IP";
        }
        try {
            final ResourcePatternResolver resolver = (ResourcePatternResolver)new PathMatchingResourcePatternResolver();
            final Resource[] resources = resolver.getResources("ip2region.db");
            if (resources.length > 0 && resources[0].exists()) {
                IpUtils.LOG.debug("\u901a\u8fc7ip2region.db\u83b7\u53d6ip");
                final InputStream inputStream = resources[0].getInputStream();
                final byte[] dbBinStr = IoUtil.readBytes(inputStream);
                final DbConfig config = new DbConfig();
                final DbSearcher searcher = new DbSearcher(config, dbBinStr);
                final DataBlock block = searcher.memorySearch(ip);
                final String region = block.getRegion();
                final String[] city = region.split("\\|");
                if (city[2].equals("0") || city[3].equals("0")) {
                    IpUtils.LOG.debug("\u83b7\u53d6\u5730\u7406\u4f4d\u7f6e\u5f02\u5e38 {}", (Object)ip);
                    return "\u5730\u5740\u5e93\u672a\u6536\u5f55";
                }
                address = region.replaceAll("0", "").replaceAll("\\|", " ");
            }
            else {
                IpUtils.LOG.debug("\u901a\u8fc7{}\u83b7\u53d6ip", (Object)"https://www.ip.cn/api/index?ip=%s&type=1");
                final String rest = HttpUtil.get(String.format("https://www.ip.cn/api/index?ip=%s&type=1", ip));
                if (StringUtils.isEmpty((CharSequence)rest)) {
                    IpUtils.LOG.debug("\u83b7\u53d6\u5730\u7406\u4f4d\u7f6e\u5f02\u5e38 {}", (Object)ip);
                    return address;
                }
                address = JSONUtil.parseObj(rest).get((Object)"address").toString();
            }
            IpUtils.LOG.debug("ip{}", (Object)ip);
        }
        catch (Exception e) {
            IpUtils.LOG.error("\u83b7\u53d6\u5730\u7406\u4f4d\u7f6e\u5f02\u5e38 {}", (Object)ip);
            e.printStackTrace();
        }
        return address;
    }

    public static byte[] textToNumericFormatV4(final String text) {
        if (text.length() == 0) {
            return null;
        }
        final byte[] bytes = new byte[4];
        final String[] elements = text.split("\\.", -1);
        try {
            switch (elements.length) {
                case 1: {
                    final long l = Long.parseLong(elements[0]);
                    if (l < 0L || l > 4294967295L) {
                        return null;
                    }
                    bytes[0] = (byte)(l >> 24 & 0xFFL);
                    bytes[1] = (byte)((l & 0xFFFFFFL) >> 16 & 0xFFL);
                    bytes[2] = (byte)((l & 0xFFFFL) >> 8 & 0xFFL);
                    bytes[3] = (byte)(l & 0xFFL);
                    break;
                }
                case 2: {
                    long l = Integer.parseInt(elements[0]);
                    if (l < 0L || l > 255L) {
                        return null;
                    }
                    bytes[0] = (byte)(l & 0xFFL);
                    l = Integer.parseInt(elements[1]);
                    if (l < 0L || l > 16777215L) {
                        return null;
                    }
                    bytes[1] = (byte)(l >> 16 & 0xFFL);
                    bytes[2] = (byte)((l & 0xFFFFL) >> 8 & 0xFFL);
                    bytes[3] = (byte)(l & 0xFFL);
                    break;
                }
                case 3: {
                    for (int i = 0; i < 2; ++i) {
                        final long l = Integer.parseInt(elements[i]);
                        if (l < 0L || l > 255L) {
                            return null;
                        }
                        bytes[i] = (byte)(l & 0xFFL);
                    }
                    final long l = Integer.parseInt(elements[2]);
                    if (l < 0L || l > 65535L) {
                        return null;
                    }
                    bytes[2] = (byte)(l >> 8 & 0xFFL);
                    bytes[3] = (byte)(l & 0xFFL);
                    break;
                }
                case 4: {
                    for (int i = 0; i < 4; ++i) {
                        final long l = Integer.parseInt(elements[i]);
                        if (l < 0L || l > 255L) {
                            return null;
                        }
                        bytes[i] = (byte)(l & 0xFFL);
                    }
                    break;
                }
                default: {
                    return null;
                }
            }
        }
        catch (NumberFormatException e) {
            return null;
        }
        return bytes;
    }

    public static String getHostIp() {
        try {
            return InetAddress.getLocalHost().getHostAddress();
        }
        catch (UnknownHostException ex) {
            return "127.0.0.1";
        }
    }

    public static String getHostName() {
        try {
            return InetAddress.getLocalHost().getHostName();
        }
        catch (UnknownHostException ex) {
            return "\u672a\u77e5";
        }
    }

    static {
        LOG = LoggerFactory.getLogger((Class)IpUtils.class);
    }
}
